﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BI.CF.Model;
using BI.CF.BLL;
using BI.CF.PageList;
using System.Text;

namespace BI.CF.Company.Controllers
{
    public class InvestController : Controller
    {
        //
        // GET: /Invest/
        Invest_BLL invest_bll = new Invest_BLL();
        public ActionResult Invest_Show()
        {
            return View();
        }

        /// <summary>
        /// 我要投资模块展示
        /// </summary>
        /// <returns></returns>
        public string Invest_Index()
        {
            int pageindex = Convert.ToInt32(Request["pageindexs"]);
            int pagesize = Convert.ToInt32(Request["pagesize"]);
            string huankuantype = Request["huankuantype"].ToString(); ;
            int jiekuanqixianstar = Convert.ToInt32(Request["jiekuanqixianstar"]);
            int jiekuanqixianend = Convert.ToInt32(Request["jiekuanqixianend"]);
            string biaotype = Request["biaotype"];
            string paixu = Request["paixu"];
            Pager<MemLoanModel> list = invest_bll.Invest_Show(pageindex, pagesize, huankuantype, jiekuanqixianstar, jiekuanqixianend, biaotype, paixu);
            string str = JsonHelper.JsonSerializer<Pager<MemLoanModel>>(list);
            return str;
        }
        public string User_Select_ID()
        {
            UserrsModel m = new UserrsModel();
            int MID = Convert.ToInt32(Request["id"]);
            m = invest_bll.Mem_select_User_ID(MID);
            string str = JsonHelper.JsonSerializer(m);
            return str;
        }
        public ActionResult Invest_One_Show()
        {
            if (Request["id"] == null)
            {
                return Content("<script>location.href='/Home/Index'</script>");

            }
            else
            {
                Session["ID"] = Request["id"].ToString();
                return View();
            }
        }
        public JsonResult User_Only()
        {
            int ID = Convert.ToInt32(Request["ID"]);
            UsersBLL bll = new UsersBLL();
            UserrsModel m = bll.Show_UserDetails(ID);
            return Json(m);
        }
        /// <summary>
        /// 填写投资界面
        /// </summary>
        /// <returns></returns>
        public ActionResult touzi()
        {
            if (Session["UModel"] != null)
            {
                int id = Convert.ToInt32(Request["idd"]);
                Session["memloanid"] = id;//借款id
                invest_bll.Memloan_Show_XiangQing(id);
                return View();
            }
            else
            {
                return Content("<script>alert('请您登陆！');location.href='/MyAccount/View_UserLogin'</script>");
            }


        }
        public string touzi_show()
        {
            int memloanid = Convert.ToInt32(Request["memid"]);
            MemLoanModel ml = invest_bll.Memloan_Show_XiangQing(memloanid);
            //获取当前登录人
            UserrsModel Umodel = Session["UModel"] as UserrsModel;
            MemPropertyModel mp = invest_bll.Memloan_Show_Memproperty(Umodel.UserId);//通过当前登录人的ID
            decimal baifenbi;
            baifenbi = (ml.AlreadyRaiseLoan / ml.LoanMoney) * 100;
            baifenbi =Convert.ToDecimal( baifenbi.ToString().Substring(0, 4));
            StringBuilder str = new StringBuilder();
            str.Append("<h2><span>投标信息确认</span></h2>");
            str.Append("<div class='hdivBid_cont border'>");
            str.Append("<div class='tishi'><strong class='red'>温馨提示：请认真核实标的信息及账户信息，一旦点击投标按钮，表示您将确认投标金额不得撤销！</strong> </div>");
            str.Append("<div class='Dotted_1'>");
            str.Append("<p><a target='_blank' href='/help/limitCategory'><b class='SubL90' title='净值标，点击查看借款类型说明'></b></a><span class='bt_txt'>" + ml.Title + "</span></p>");
            str.Append("<div class='clear'></div>");
            str.Append("</div>");
            str.Append("<ul class=''><li><p class='p_01'>借款用户：<input id='MemLoanID' value='" + ml.MemLoanId + "' type='hidden' /></p><p><span class='font_ya'>" + mp.UserName + "</span></p></li>");
            str.Append("<li><p class='p_01'>借款利率：</p><p><span class='font_ya'>" + ml.Annualrate + "%/年</span><input id='TouZiLiLv' value='" + ml.Annualrate + "' type='hidden' /></p></li>");
            str.Append("<li><p class='p_01'>借款总额：</p><p><span class='font_ya'>￥" + ml.LoanMoney + "元</span></p></li>");
            str.Append("<li><p class='p_01'>借款期限：</p><p><span class='font_ya'>" + ml.LoanRaise + "个月</span></p></li>");
            str.Append("<li><p class='p_01' style=' margin:5px 0;'>当前进度：</p><p class='Load'><em style='width:" + baifenbi + "%;'></em></p><span class='font_ya font_14 lf' style=' margin:3px 0 0 3px;'>" + baifenbi + "%</span></li>");
            str.Append("<li><p class='p_01'>可投范围：<input id='ketoufanwei' value='" + (ml.LoanMoney - ml.AlreadyRaiseLoan) + "' type='hidden' /></p><p><span class='font_ya'>￥50.00 ~ ￥" + (ml.LoanMoney - ml.AlreadyRaiseLoan) + "元</span></p></li>");
            str.Append(" <li><p class='p_01'>您的可用余额：</p><p><span class='font_ya red'>￥</span><span id='uamt' class='font_ya red'>" + mp.AvailableBalance + "</span>元 </p><a href='#' class='refresh lf' onclick='getAmt();' title='点此刷新可用余额'></a></li></ul>");
            str.Append(" <ul class=''><form id='onBiddingSubmitForm' action='/credit0/bid/onBiddingSubmit' method='post'>");
            //   str.Append("<li style='width:700px;'><p class='p_01'></p><p>您最多还可以再投<b class='red font_16 font_ya normal'>￥</b><b id='remainderamt' class='red font_16 font_ya normal'>0.00</b>元，<span style='cursor: pointer; margin-left:5px; color: #00f;' title='点击我自动填入最佳投标金额。' onclick='setMaxAmt()'>点我填入最佳投标金额</span></p></li>");
            str.Append(" <li style='width:700px;'><p class='p_01'>投标金额：</p><p><span><input type='text' class='txt_180 font_ya font_16' name='txtBidAmt' id='txtBidAmt'></span><span><a href='https://member.my089.com/pay/deposit' target='_blank' class='bule'>我要充值</a></span></p></li>");
            // str.Append("<li style='width:700px;'><p class='p_01'>大写金额：</p><p><span style=' line-height:30px;' id='spanDaXieAmount'></span></p></li>");
            str.Append("<li style='width:700px;'><p class='p_01'>");
            str.Append("<input type='hidden' value='17030520-0605-4649-4600-830017917595' id='hdnSID' name='hdnSID'>");
            str.Append("<input type='hidden' value='0' id='hdnIsBlack' name='hdnIsBlack'>");
            str.Append("<input id='__TOKEN__' name='__TOKEN__' type='hidden' value='MVH9WDQR6H1LKM8ZEQQI7S0YTPY7TIRU' /><input name='__TOKEN__ID__' type='hidden' value='zHwm7VSVbpfoImxm' /><!--防止重复提交参数--></p>");
            str.Append("<p><span><input type='button' value='确认无误，投标' onclick='touziint_get()' class='Bid_but' name='btnBid' id='btnBid'></span></p>");
            str.Append("</li><li style='width:700px;'><p class='p_01 red'>注：</p><p><span class='red' style='line-height:30px;'>手动投标为50元起投、1元递增；最终有效金额以实际为准。</span></p></li>");
            str.Append("</form>");
            str.Append("</ul>");
            str.Append("<div class='clear'></div>");
            str.Append("</div>");

            //string str = jsonhelper.jsonserializer(ml);
            //string str2 = jsonhelper.jsonserializer(mp);
            return str.ToString();
        }
        /// <summary>
        /// 查询投资人的账户资金是否充足
        /// </summary>
        /// <returns></returns>
        public decimal User_Select_TouZiRen_Money()
        {
            //获取当前登录人的ID(等待完成)
            //获取当前登录人
            if (Session["UModel"] != null)
            {
                UserrsModel Umodel = Session["UModel"] as UserrsModel;
                decimal mo = invest_bll.AvailableBalance(Umodel.UserId);
                return mo;
            }
            else
            {
                return -1;
            }

        }
        /// <summary>
        /// 进行投资
        /// </summary>
        /// <returns></returns>
        public bool TouZi_Gets()
        {
            //获取当前登录人
            UserrsModel Umodel = Session["UModel"] as UserrsModel;
            int UserID = Convert.ToInt32(Umodel.UserId);
            decimal TouZiMoney = Convert.ToDecimal(Request["TouZiJinMoney"]);
            string TouZiLiXi = Request["TouZiLiXi"].ToString();
            string TouZiStarTime = Request["TouBiaoTime"].ToString();
            int MemLoanID = Convert.ToInt32(Request["Memloanid"]);
            bool i = invest_bll.TouZi_Geting(UserID, TouZiMoney, TouZiLiXi, TouZiStarTime, MemLoanID);
            return i;
        }
    }
}
